package net.qiujuer.genius.ui.utils;

import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;


import net.qiujuer.genius.ui.ResourceTable;

import java.io.IOException;


/**
 * 自定义Toast弹出框
 */
public final class ToastUtil {
//    private HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x000224, "ToastUtil");
    private int cornerRadius;
    private Color backgroundColor;
    private Color strokeColor;
    private int strokeWidth;
    private int iconStart;
    private int iconEnd;
    private Color textColor;
    private int font;
    private int length;
    private int style;
    private int textSize;
    private boolean isTextSizeFromStyleXml = false;
    private boolean solidBackground;
    private boolean textBold;
    private String text;
    private int gravity;
    private int mCornerRadius = 60;
    private Context context;
    private ToastDialog toast;
    private Component rootLayout = null;
    private Text textView;

    private ToastUtil(Context context) {
    }


    /**
     * 显示动画
     *
     * @return AnimatorProperty AnimatorProperty
     */
    private AnimatorProperty createAnimShow() {
        AnimatorProperty animator = new AnimatorProperty();
        animator.alphaFrom(0.7f).alpha(1).setDuration(500);
        return animator;
    }

    /**
     * 显示
     */
    public void show() {
        synchronized (ToastUtil.class) {
            if (toast == null || rootLayout == null) {
                rootLayout = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_toast_layout, null, false);
                textView = (Text) rootLayout.findComponentById(ResourceTable.Id_textview);
                toast = new ToastDialog(context);
                toast.setSize(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
                toast.setDestroyedListener(new CommonDialog.DestroyedListener() {
                    @Override
                    public void onDestroy() {

                    }
                });
            } else {
                toast.destroy();
            }
            setLayoutBackground(rootLayout);
            setTextView();
            setIcon();
            toast.setAlignment(gravity);
            toast.setCornerRadius(AttrHelper.fp2px(mCornerRadius, context));
            toast.setOffset(0, AttrHelper.fp2px(50, context));
            if (toast.getComponent() != null && toast.getComponent().getComponentParent() != null) {
                ((ComponentContainer) toast.getComponent().getComponentParent()).removeAllComponents();
            }
            toast.setComponent(rootLayout);
            toast.show();
            AnimatorProperty animatorProperty = createAnimShow();
            animatorProperty.setTarget(rootLayout);
            animatorProperty.start();
        }
    }

    /**
     * 设置前后图片
     */
    private void setIcon() {
        int top = (int) AttrHelper.fp2px(0, context);
        //paddingNoIcon
        int left = (int) AttrHelper.fp2px(10, context);
        //paddingHorizontal1
        int right = (int) AttrHelper.fp2px(10, context);
        int iconSize = (int) AttrHelper.fp2px(20, context);
        textView.setAroundElementsRelative(null, null, null, null);
        if (iconStart != 0) {
            PixelMapElement drawable = getPixelMapElement(iconStart);
            if (drawable != null) {
                drawable.setBounds(0, 0, iconSize, iconSize);
                textView.setAroundElementsRelative(drawable, null, null, null);
                if (isRTL()) {
                    rootLayout.setPadding(left, top, right, top);
                } else {
                    rootLayout.setPadding(right, top, left, top);
                }
            }
        }

        if (iconEnd != 0) {
            PixelMapElement drawable = getPixelMapElement(iconEnd);
            if (drawable != null) {
                drawable.setBounds(0, 0, iconSize, iconSize);
                textView.setAroundElementsRelative(null, null, drawable, null);
                if (isRTL()) {
                    rootLayout.setPadding(left, top, left, top);
                } else {
                    rootLayout.setPadding(left, top, right, top);
                }
            }
        }
        if (iconStart != 0 && iconEnd != 0) {
            PixelMapElement drawableLeft = getPixelMapElement(iconStart);
            PixelMapElement drawableRight = getPixelMapElement(iconEnd);
            if (drawableLeft != null && drawableRight != null) {
                drawableLeft.setBounds(0, 0, iconSize, iconSize);
                drawableRight.setBounds(0, 0, iconSize, iconSize);
                textView.setAroundElements(drawableLeft, null, drawableRight, null);
                rootLayout.setPadding(right, top, right, top);
            }
        }

    }

    private PixelMapElement getPixelMapElement(int mIcon) {
        PixelMapElement drawable = null;
        Resource resource = null;
        try {
            resource = context.getResourceManager().getResource(mIcon);
            drawable = new PixelMapElement(resource);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        }
        return drawable;
    }

    /**
     * 设置文本样式
     */
    private void setTextView() {
        textView.setText(text);
        if (textColor != null) {
            textView.setTextColor(textColor);
        }
        if (textSize > 0) {
            textView.setTextSize(textSize, isTextSizeFromStyleXml ? Text.TextSizeType.PX : Text.TextSizeType.VP);
        }
        if (font > 0) {
            if (textBold) {
                textView.setFont(Font.DEFAULT_BOLD);
            } else {
                textView.setFont(Font.DEFAULT);
            }
        }
        //   return  drawable;
    }

    static boolean isRTL() {
        return true;
    }


    /**
     * setLayoutBackground
     *
     * @param rootLayout rootLayout
     */
    private void setLayoutBackground(Component rootLayout) {
        ShapeElement mBackgroundDrawable = new ShapeElement();
        mBackgroundDrawable.setAlpha(230);
        mBackgroundDrawable.setCornerRadius(AttrHelper.fp2px(mCornerRadius, context));
        if (strokeWidth > 0) {
            mBackgroundDrawable.setStroke(AttrHelper.fp2px(strokeWidth, context), RgbColor.fromArgbInt(strokeColor.getValue()));
        }
        if (cornerRadius > -1) {
            mBackgroundDrawable.setCornerRadius(AttrHelper.fp2px(cornerRadius, context));
        }
        if (backgroundColor != null) {
            mBackgroundDrawable.setRgbColor(RgbColor.fromArgbInt(backgroundColor.getValue()));
        }
        if (solidBackground) {
            mBackgroundDrawable.setAlpha(255);
        }
        rootLayout.setBackground(mBackgroundDrawable);
    }


    /**
     * 获取构建者
     *
     * @param context 上下文
     * @return Builder Builder
     */
    public static Builder builder(Context context) {
        return new Builder(context);
    }

    private ToastUtil(Builder builder) {
        //super(builder.context);
        this.context = builder.context;
        this.backgroundColor = builder.params.backgroundColor;
        this.cornerRadius = builder.params.cornerRadius;
        this.iconEnd = builder.params.iconEnd;
        this.iconStart = builder.params.iconStart;
        this.strokeColor = builder.params.strokeColor;
        this.strokeWidth = builder.params.strokeWidth;
        this.solidBackground = builder.params.solidBackground;
        this.textColor = builder.params.textColor;
        this.textSize = builder.params.textSize;
        this.textBold = builder.params.textBold;
        this.font = builder.params.font;
        this.text = builder.params.text;
        this.gravity = builder.params.gravity;
        this.length = builder.params.length;
    }


    final static class Params {
        public int cornerRadius = -1;
        public Color backgroundColor;
        public Color strokeColor;
        public int strokeWidth;
        public int iconStart;
        public int iconEnd;
        public Color textColor;
        public int font;
        public int length;
        public int textSize;
        public boolean solidBackground;
        public boolean textBold;
        public String text;
        public int gravity = LayoutAlignment.CENTER;


    }

    /**
     * 构造基类
     */
    public static class Builder {
        private Params params = new Params();
        /**
         * 上下文
         */
        public final Context context;

        /**
         * Builder
         *
         * @param context 上下文
         */
        public Builder(Context context) {
            this.context = context.getApplicationContext();
        }

        /**
         * text
         *
         * @param text 文本
         * @return Builder Builder
         */
        public Builder text(String text) {
            this.params.text = text;
            return this;
        }

        /**
         * textColor
         *
         * @param textColor textColor
         * @return Builder Builder
         */
        public Builder textColor(Color textColor) {
            this.params.textColor = textColor;
            return this;
        }

        /**
         * textBold
         *
         * @return Builder
         */
        public Builder textBold() {
            this.params.textBold = true;
            return this;
        }

        /**
         * textSize
         *
         * @param textSize textSize
         * @return Builder Builder
         */
        public Builder textSize(int textSize) {
            this.params.textSize = textSize;
            return this;
        }

        /**
         * font
         * @param font A font resource id like R.font.somefont as introduced with the new font api
         * @return Builder Builder
         */
        public Builder font(int font) {
            this.params.font = font;
            return this;
        }

        /**
         * backgroundColor
         *
         * @param backgroundColor backgroundColor
         * @return Builder Builder
         */
        public Builder backgroundColor(Color backgroundColor) {
            this.params.backgroundColor = backgroundColor;
            return this;
        }

        /**
         * This call will make the StyleableToast's background completely solid without any opacity.
         *
         * @return Builder Builder
         */
        public Builder solidBackground() {
            this.params.solidBackground = true;
            return this;
        }

        /**
         * stroke
         *
         * @param strokeWidth strokeWidth
         * @param strokeColor strokeColor
         * @return Builder Builder
         */
        public Builder stroke(int strokeWidth, Color strokeColor) {
            this.params.strokeWidth = strokeWidth;
            this.params.strokeColor = strokeColor;
            return this;
        }

        /**
         * cornerRadius
         *
         * @param cornerRadius Sets the corner radius of the StyleableToast's shape.
         * @return Builder Builder
         */
        public Builder cornerRadius(int cornerRadius) {
            this.params.cornerRadius = cornerRadius;
            return this;
        }

        /**
         * iconStart
         *
         * @param iconStart iconStart
         * @return Builder Builder
         */
        public Builder iconStart(int iconStart) {
            this.params.iconStart = iconStart;
            return this;
        }

        /**
         * iconEnd
         *
         * @param iconEnd iconEnd
         * @return Builder Builder
         */
        public Builder iconEnd(int iconEnd) {
            this.params.iconEnd = iconEnd;
            return this;
        }

        /**
         * Sets where the StyleableToast will appear on the screen
         *
         * @param gravity gravity
         * @return Builder Builder
         */
        public Builder gravity(int gravity) {
            this.params.gravity = gravity;
            return this;
        }

        /**
         * length
         *
         * @param length length
         * @return Builder Builder
         */
        public Builder length(int length) {
            this.params.length = length;
            return this;
        }

        /**
         * show
         */
        public void show() {
            new ToastUtil(this).show();
        }
    }

    /**
     * showToast
     *
     * @param context 上下文
     * @param textMsg 文本内容
     * @param backgroundColor 背景颜色
     * @param startIcon 左边加载图片
     * @param endIcon 右边加载图片
     */
    public static void showToast(Context context, String textMsg, Color backgroundColor, int startIcon, int endIcon) {
        ToastUtil.builder(context)
            .text(textMsg)
            .backgroundColor(backgroundColor)
            .solidBackground()
            .textColor(Color.WHITE)
            .textBold()
            .cornerRadius(40)
            .textSize(15)
            .iconStart(startIcon)
            .iconEnd(endIcon)
            .show();
    }

    /**
     * showToasterror
     *
     * @param context 上下文
     * @param textMsg 文本内容
     * @param backgroundColor 背景颜色
     * @param startIcon 左边加载图片
     * @param endIcon 右边加载图片
     */
    public static void showToasterror(Context context, String textMsg, Color backgroundColor, int startIcon, int endIcon) {
        ToastUtil.builder(context)
            .text(textMsg)
            .backgroundColor(backgroundColor)
            .solidBackground()
            .textColor(Color.WHITE)
            .textBold()
            .cornerRadius(40)
            .textSize(15)
            .gravity(LayoutAlignment.BOTTOM)
            .iconStart(startIcon)
            .iconEnd(endIcon)
            .show();
    }

    /**
     * showToastB0ttom
     *
     * @param context 上下文
     * @param textMsg 文本内容
     * @param backgroundColor 背景颜色
     * @param startIcon 左边加载图片
     * @param endIcon 右边加载图片
     */
    public static void showToastB0ttom(Context context, String textMsg, Color backgroundColor, int startIcon, int endIcon) {
        ToastUtil.builder(context)
            .text(textMsg)
            .backgroundColor(backgroundColor)
            .solidBackground()
            .textColor(Color.WHITE)
            .textBold()
            .cornerRadius(40)
            .textSize(15)
            .gravity(LayoutAlignment.BOTTOM)
            .iconStart(startIcon)
            .iconEnd(endIcon)
            .show();
    }

    /**
     * showToastB0ttom
     *
     * @param context 上下文
     * @param textMsg 文本内容
     * @param backgroundColor 背景颜色
     * @param textColor 文本颜色
     * @param textSize 文本字体大小
     */
    public static void showToastB0ttom(Context context, String textMsg, Color backgroundColor, Color textColor, int textSize) {
        ToastUtil.builder(context)
            .text(textMsg)
            .backgroundColor(backgroundColor)
            .solidBackground()
            .textColor(textColor)
            .textBold()
            .cornerRadius(40)
            .textSize(textSize)
            .gravity(LayoutAlignment.BOTTOM)
            .show();
    }

    /**
     * showToast
     *
     * @param context 上下文
     * @param textMsg 文本内容
     * @param startIcon 左边加载图片
     * @param endIcon 右边加载图片
     */
    public static void showToast(Context context, String textMsg, int startIcon, int endIcon) {
        ToastUtil.builder(context)
            .text(textMsg)
            .backgroundColor(new Color(AppCfig.h_black))
            .solidBackground()
            .textColor(Color.WHITE)
            .textBold()
            .cornerRadius(40)
            .textSize(15)
            .iconStart(startIcon)
            .iconEnd(endIcon)
            .show();
    }


    /**
     * showToastCENTER
     *
     * @param context 上下文
     * @param textMsg 文本内容
     */
    public static void showToastCENTER(Context context, String textMsg) {
        ToastUtil.builder(context)
            .text(textMsg)
            .backgroundColor(new Color(AppCfig.h_black))
            .solidBackground()
            .textColor(Color.WHITE)
            .textBold()
            .cornerRadius(40)
            .textSize(15)
            .show();
    }

    /**
     * showToast
     *
     * @param context 上下文
     * @param textMsg 文本内容
     */
    public static void showToast(Context context, String textMsg) {
        ToastUtil.builder(context)
            .gravity(LayoutAlignment.BOTTOM)
            .text(textMsg)
            .backgroundColor(new Color(AppCfig.h_black))
            .solidBackground()
            .textColor(Color.WHITE)
            .textBold()
            .cornerRadius(40)
            .textSize(15)
            .show();
    }

    /**
     * showToast
     *
     * @param context
     * @param textMsg
     * @param textSize 文字大小
     */
    public static void showToast(Context context, String textMsg, int textSize) {
        ToastUtil.builder(context)
            .text(textMsg)
            .backgroundColor(new Color(AppCfig.h_black))
            .solidBackground()
            .textColor(Color.WHITE)
            .textBold()
            .cornerRadius(40)
            .textSize(textSize)
            .show();
    }

}
